﻿using System.Net;
using System.Web;
using System.Web.Mvc;
using Smarly.Bussines.Membership;
using Smarly.Web.Areas.Profile.Helpers;

namespace Smarly.Web.ActionFilters
{
	public class AuthorizeSmarlyAttribute : AuthorizeAttribute
	{
		protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
		{
			filterContext.Result = new AuthResult();
/*
			HttpResponseBase response = filterContext.HttpContext.Response;
			HttpRequestBase request = filterContext.HttpContext.Request;
			response.Cookies.Add(new HttpCookie("authref", request.Url.ToString()) { Path = "/", Domain = request.AuthDomain() });

			if (request.UrlReferrer != null)
				response.Cookies.Add(new HttpCookie("authurlref", request.UrlReferrer.ToString()) { Path = "/", Domain = request.AuthDomain() });

			response.Redirect(new UrlHelper(filterContext.RequestContext).AccountLogin(), true);
*/
//			base.HandleUnauthorizedRequest(filterContext);
		}

		private class AuthResult : ActionResult{
			public override void ExecuteResult(ControllerContext context)
			{
			HttpResponseBase response = context.HttpContext.Response;
			HttpRequestBase request = context.HttpContext.Request;
			response.Cookies.Add(new HttpCookie("authref", request.Url.ToString()) { Path = "/", Domain = request.AuthDomain() });

			if (request.UrlReferrer != null)
				response.Cookies.Add(new HttpCookie("authurlref", request.UrlReferrer.ToString()) { Path = "/", Domain = request.AuthDomain() });

			context.HttpContext.Response.Redirect(new UrlHelper(context.RequestContext).AccountLogin(), true);
				//context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
			}
		}
	}
}